package com.jie.deletefile.Gestures;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.jie.deletefile.R;

import java.util.List;


public class SetActivity extends AppCompatActivity {

    private android.widget.TextView tvshow;
    private android.widget.LinearLayout activityset;
    private GestureLockViewGroup set;
    private final int FIRSTSET = 1;//第一次绘制
    private final int SECONDSET = 2;//第二次绘制

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        this.set = (GestureLockViewGroup) findViewById(R.id.set);
        this.tvshow = (TextView) findViewById(R.id.tv_show);
        tvshow.setText("第一次绘制");
        set.setOnGestureLockViewListener(new GestureLockViewGroup.OnGestureLockViewListener() {
            @Override
            public void onBlockSelected(int cId) {

            }

            /**
             * 绘制结束监听
             * @param matched
             */
            @Override
            public void onGestureEvent(boolean matched) {
                Log.e("TAG", "onGestureEvent: mtry"+set.getmTryTimes()+"    max"+set.getmTryMaxTimes());
                if (set.getmTryTimes() == 1) {
                    //第一次绘制
                    handler.sendEmptyMessage(FIRSTSET);
                } else {
                    //第二次绘制
                    Message msg = handler.obtainMessage();
                    msg.what = SECONDSET;
                    msg.obj = matched;//两次绘制是否相同
                    handler.sendMessage(msg);
                }


            }

            @Override
            public void onUnmatchedExceedBoundary() {

            }
        });

    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case FIRSTSET:
                    List<Integer> getchoosed = set.getchoosed();
                    Log.e("TAG", "handleMessage: "+getchoosed.toArray());
                    Integer[] array = DataUtils.list2integer(getchoosed);


//                    Integer[] integers = (Integer[]) getchoosed.toArray();
//                    Integer[] array = (Integer[]) getchoosed.toArray();
                    //将第一次绘制的结果作为手势结果
                    set.setAnswer(array);

                    set.reset();//刷新手势视图,清理绘制线条
                    tvshow.setText("第二次绘制");
                    break;
                case SECONDSET:
                    boolean isRight = (boolean) msg.obj;
                    if (isRight) {
                        /**
                         * 将绘制成功的结果传入sp存储
                         */
                        List<Integer> getchoosed1 = set.getchoosed();
                        Integer[] array1 = DataUtils.list2integer(getchoosed1);
                        String s = DataUtils.intToString(array1);
                        s = s.trim();
                        Log.e("TAG", "handleMessage: s="+s );
                        Toast.makeText(SetActivity.this, "手势密码设置成功", Toast.LENGTH_SHORT).show();
                        SharedPreferences sp = getSharedPreferences("shoushi", MODE_PRIVATE);
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putBoolean("isSetted", true);
                        edit.putString("answer", s);
                        edit.commit();

                        finish();
                    } else {
                        Toast.makeText(SetActivity.this, "两次绘制不一致,请重新绘制", Toast.LENGTH_SHORT).show();
                        set.resetmTryTimes();
                        tvshow.setText("第一次绘制");
                    }
            }
        }
    };





}
